package user;

import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import utils.DomainDAORemote;
import utils.UserDAORemote;
import utils.UsertypeDAORemote;
import dtos.UserDTO;

/**
 * 
 * @author figans
 * 
 *         Contains getters and setter for the edit profile page
 */
@ManagedBean
@RequestScoped
public class EditProfileBean {

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());
	
	@EJB
	UserDAORemote userDAO;

	@EJB
	DomainDAORemote domainDAO;

	@EJB
	UsertypeDAORemote usertypeDAO;

	// @ManagedProperty(value = "#{userDTO}")
	UserDTO userDTO;

	String pass;
	String confPass;
	// String verifPass;

	String userDomain;
	String usertype;

	String errorMsg = "";
	
	String confMsg;
	String confPassmsg;

	boolean isError;

	@PostConstruct
	public void init() {
//		userDTO = userDAO.getUser(2);
		userDTO = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		confMsg="";
		confPassmsg="";
	}

	public UserDTO getUserDTO() {
		userDTO = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		System.out.println("userType id in session is " + userDTO);
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	
	public String getConfMsg() {
		return confMsg;
	}

	public void setConfMsg(String confMsg) {
		this.confMsg = confMsg;
	}

	/**
	 * ALBERTTTT SET ID USEEEEER
	 * 
	 * 
	 * 
	 * 
	 * @return
	 */

	// public UserDTO getUserDTO() {
	// if (userDTO == null) {
	// /*
	// * La integrare stergem linia de mai jos si o inlocuim cu userDTO
	// * =(UserDTO) FacesContext.getCurrentInstance().getExternalContext()
	// * .getSessionMap().get("userDTO");
	// * THIS IS DONE :X
	// */
	//
	// // userDTO = (UserDTO) FacesContext.getCurrentInstance()
	// // .getExternalContext().getSessionMap().get("loggedUser");
	// // System.out.println("userType id in session is "+userDTO.getName());
	//
	// }
	// return userDTO;
	// }

	// public void setUserDTO(UserDTO userDTO) {
	// this.userDTO = userDTO;
	//
	// }

	public String getErrorMsg() {
		return errorMsg;
	}

	public String getConfPassmsg() {
		return confPassmsg;
	}

	public void setConfPassmsg(String confPassmsg) {
		this.confPassmsg = confPassmsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getConfPass() {
		return confPass;
	}

	public void setConfPass(String confPass) {
		this.confPass = confPass;
	}

	// public String getVerifPass() {
	// return verifPass;
	// }
	//
	// public void setVerifPass(String verifPass) {
	// this.verifPass = verifPass;
	// }

	public String updateUser() {

		// setErrorMsg(getErrorMsg() + userDAO.updateUser(userDTO, verifPass));
		setErrorMsg(getErrorMsg() + userDAO.updateUserProfile(userDTO));
		if(errorMsg.equals(""))
		{
			confMsg=bundle.getString("Beans_user_editProfileBean_confEdit");
		}
		return "ok";
	}

	public String updatePassword() {

		if (pass.equals(confPass)) {
			errorMsg = userDAO.updateUserPassword(userDTO, pass);
			confPassmsg=bundle.getString("Beans_user_editProfileBean_confChange");
		} else {
			errorMsg = bundle.getString("Beans_user_editProfileBean_passmismatch");
		}
		return "ok";
	}

	public boolean getIsError() {
		if (errorMsg.equals("")) {
			isError = false;
		} else {
			isError = true;
		}
		return isError;
	}

	public void setIsError(boolean isError) {
		this.isError = isError;
	}

	public String getUserDomain() {
		return domainDAO.getDomain(userDTO.getIdDomain()).getName();
	}

	public void setUserDomain(String userDomain) {
		this.userDomain = userDomain;
	}

	public String getUsertype() {

		return usertypeDAO.getUsertype(userDTO.getIdUsertype()).getName();
	}

	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}

}
